package com.lswms.modules.lswms.controller.receipt;

import com.alibaba.druid.util.StringUtils;
import com.lswms.common.annotation.LogOperation;
import com.lswms.common.constant.Constant;
import com.lswms.common.page.PageData;
import com.lswms.common.utils.ExcelUtils;
import com.lswms.common.utils.Result;
import com.lswms.common.validator.AssertUtils;
import com.lswms.common.validator.ValidatorUtils;
import com.lswms.common.validator.group.AddGroup;
import com.lswms.common.validator.group.DefaultGroup;
import com.lswms.common.validator.group.UpdateGroup;
import com.lswms.modules.lswms.MEGConstant;
import com.lswms.modules.lswms.dto.receipt.ReceiptDTO;
import com.lswms.modules.lswms.dto.receipt.ReceiptdetailDTO;
import com.lswms.modules.lswms.entity.receipt.ReceiptdetailEntity;
import com.lswms.modules.lswms.excel.receipt.ReceiptdetailExcel;
import com.lswms.modules.lswms.service.receipt.ReceiptdetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 收货明细
 *
 * @author DX
 * @since 1.0.0 2021-06-13
 */
@RestController
@RequestMapping("lswms/receiptdetail")
@Api(tags = "收货明细")
public class ReceiptdetailController {
	@Autowired
	private ReceiptdetailService receiptdetailService;

	@GetMapping("page")
	@ApiOperation("分页")
	@ApiImplicitParams({
			@ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
			@ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
			@ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String") })
//	@RequiresPermissions("lswms:receiptdetail:page")
	public Result<PageData<ReceiptdetailEntity>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
		try {
			PageData<ReceiptdetailEntity> page = receiptdetailService.pageBySql(params);

			return new Result<PageData<ReceiptdetailEntity>>().ok(page);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result().error(MEGConstant.IS_ERROR);
		}
	}

	@GetMapping("{id}")
	@ApiOperation("信息")
//	@RequiresPermissions("lswms:receiptdetail:info")
	public Result<ReceiptdetailEntity> get(@PathVariable("id") Long id) {
		try {
			ReceiptdetailEntity data = receiptdetailService.getBySql(id);

			return new Result<ReceiptdetailEntity>().ok(data);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result().error(MEGConstant.IS_ERROR);
		}
	}

	
	@PostMapping
	@ApiOperation("保存")
	@LogOperation("保存")
	@RequiresPermissions("lswms:receipt:save")
	public Result save(@RequestBody ReceiptdetailDTO dto) {
		try {
			// 效验数据
			ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

			// 验证数据
			String msg = receiptdetailService.validAndSave(dto);
			if (msg != null) {
				return new Result().error(msg);
			}
			// 添加行号
//			Integer line = receiptdetailService.getReceiptdetailLine(dto.getReceiptkey());
//
//			dto.setLine(line + 1);
//			receiptdetailService.save(dto);

			return new Result();
		} catch (Exception e) {
			e.printStackTrace();
			return new Result().error(MEGConstant.IS_ERROR);
		}
	}

	@PutMapping
	@ApiOperation("修改")
	@LogOperation("修改")
	@RequiresPermissions("lswms:receipt:update")
	public Result update(@RequestBody ReceiptdetailDTO dto) {
		try {
			// 效验数据
			ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

			// 验证数据
			String msg = receiptdetailService.validSave(dto);
			if (msg != null) {
				return new Result().error(msg);
			}

			receiptdetailService.update(dto);

			return new Result();
		} catch (Exception e) {
			e.printStackTrace();
			return new Result().error(MEGConstant.IS_ERROR);
		}
	}

	@DeleteMapping
	@ApiOperation("删除")
	@LogOperation("删除")
	@RequiresPermissions("lswms:receipt:delete")
	public Result delete(@RequestBody Long[] ids) {
		try {
			// 效验数据
			AssertUtils.isArrayEmpty(ids, "id");

			for (Long id : ids) {
				ReceiptdetailDTO receiptdetailDTO = receiptdetailService.get(id);
				Integer status = receiptdetailDTO.getStatus();
				if (status != 0) {
					return new Result().error("非新建状态，不容许删除");
				}

				String msg = receiptdetailService.validSave(receiptdetailDTO);
				if (msg != null) {
					return new Result().error(msg);
				}
			}
			receiptdetailService.delete(ids);

			return new Result();
		} catch (Exception e) {
			e.printStackTrace();
			return new Result().error(MEGConstant.IS_ERROR);
		}
	}

	@GetMapping("export")
	@ApiOperation("导出")
	@LogOperation("导出")
	@RequiresPermissions("lswms:receipt:export")
	public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response)
			throws Exception {
		try {
			List<ReceiptdetailDTO> list = receiptdetailService.list(params);

			ExcelUtils.exportExcelToTarget(response, null, list, ReceiptdetailExcel.class);
		} catch (Exception e) {
			e.printStackTrace();
			// return new Result().error(MEGConstant.IS_ERROR);
		}
	}

	@GetMapping("count/{receiptkey}")
	@ApiOperation("获取收货单明细的行数")
	public Result<ReceiptdetailDTO> get(@PathVariable("receiptkey") String receiptkey) {
		try {
			Integer count = receiptdetailService.getReceiptdetailCount(receiptkey);

			return new Result().ok(count);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result().error(MEGConstant.IS_ERROR);
		}
	}

}
